//
//  YWAttendanceRecordTableViewCell.m
//  maintain
//
//  Created by 烟雨云渊 on 16/8/6.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWAttendanceRecordTableViewCell.h"
#import "YWMacro.h"
#import "Masonry.h"
#import "YWCommonTool.h"
#import "YYLabel.h"

#import "YWAttendanceRecord.h"
#import "YWAttendanceManager.h"
#import "YWEquipmentManager.h"
#import "YWBuildingManager.h"

@interface YWAttendanceRecordTableViewCell()
/* 状态 */
@property (weak, nonatomic) UIImageView *statusView;
/* 签到 */
@property (weak, nonatomic) YYLabel *signLabel;
/* 时间 */
@property (weak, nonatomic) YYLabel *timeLabel;
/* 地点 */
@property (weak, nonatomic) YYLabel *locationLabel;

@end

@implementation YWAttendanceRecordTableViewCell

+ (instancetype)cellWithTableView:(UITableView *)tableVie{
 
    YWAttendanceRecordTableViewCell *cell = [tableVie dequeueReusableCellWithIdentifier:YWAttendanceRecordTableViewCellIdentifier];
    
    if (cell == nil) {
        cell = [[YWAttendanceRecordTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:YWAttendanceRecordTableViewCellIdentifier];
    }
    return cell;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setUpSubviews];
    }
    
    return self;
}

- (void)setUpSubviews{
    [self.statusView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView).offset(6);
        make.centerY.equalTo(self.signLabel.mas_centerY);
        make.width.equalTo(@23);
        make.height.equalTo(@23);
    }];
    
    [self.signLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.statusView.mas_right).offset(6);
        make.centerY.equalTo(self.locationLabel.mas_centerY);
        make.width.equalTo(@35);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.signLabel.mas_right).offset(8);
        make.centerY.equalTo(self.locationLabel.mas_centerY);
        make.width.equalTo(@90);
    }];
    
    [self.locationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView).offset(10);
        make.left.mas_equalTo(self.timeLabel.mas_right).offset(kMarginW);
        make.right.mas_equalTo(self.contentView).offset(-kMarginW);
        make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-10);
    }];
}

- (UIImageView *)statusView{
    if (!_statusView) {
        UIImageView * imageView = [[UIImageView alloc] init];
        [self.contentView addSubview:imageView];
        _statusView = imageView;
    }
    return _statusView;
}

- (YYLabel *)signLabel{
    if (!_signLabel) {
        YYLabel * label = [[YYLabel alloc] init];
        label.font = kContentFont;
        [self.contentView addSubview:label];
        _signLabel = label;
    }
    return _signLabel;
}

- (YYLabel *)timeLabel{
    if (!_timeLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        [self.contentView addSubview:label];
        _timeLabel = label;
    }
    return _timeLabel;
}

- (YYLabel *)locationLabel{
    if (!_locationLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.numberOfLines = 0;
        [self.contentView addSubview:label];
        _locationLabel = label;
    }
    return _locationLabel;
}
- (void)setAttendanceRecord:(YWAttendanceRecord*)attendanceRecord{
    _attendanceRecord = attendanceRecord;
    if (attendanceRecord.uploadStatus == YWAttendanceUploadStatusSucceed) {
        self.statusView.image = [UIImage imageNamed:@"trouble_ok_select"];
    }else if(attendanceRecord.uploadStatus == YWAttendanceUploadStatusFailure ||attendanceRecord.uploadStatus == YWAttendanceUploadStatusChangeTime){
        self.statusView.image = [UIImage imageNamed:@"trouble_no_select"];
    }else{
        self.statusView.image = [UIImage imageNamed:@"uploading"];
    }
    self.signLabel.text = attendanceRecord.type == YWAttendanceTypeArrive ? NSLocalizedString(@"arrive", nil) : NSLocalizedString(@"leave", nil);
    self.timeLabel.text = [YWCommonTool showTime:attendanceRecord.arriveTime/1000 andFormatter:KTimeFormatShortTimeDate];
    //显示的是二维码所在的地址
    //通过二维码Id获取location，位置
//    NSInteger locationId = attendanceRecord.qrcodeId.length ? [[YWEquipmentManager shareManager] locationIdWithQrcodeId:attendanceRecord.qrcodeId] :attendanceRecord.locationId;
//    NSString *location = [[YWBuildingManager shareManager] areaLocationWithLocationId:locationId];
    
    self.locationLabel.text = _attendanceRecord.locationText.length>0?_attendanceRecord.locationText : NSLocalizedString(@"none", nil);
    
    self.locationLabel.preferredMaxLayoutWidth = self.locationLabel.frame.size.width;
}

@end
